package main

import "fmt"

func main() {
	var a [5]int // 创建一个长度为5的int数组，默认值为0
	a[0] = 10
	fmt.Printf("a[0] = %d\n", a[0])

	var num = []int{1, 2, 3, 4, 5, 6}
	num[0] = 1
	num[1] = 2
	num[2] = 3

	for k, v := range num {
		fmt.Printf("index = %d, value = %d\n", k, v)
	}

	nums := [5]int{1, 2, 3, 4, 5}
	fmt.Println(nums)

	arr := make([]int, 5) // 创建长度为5的 int 切片
	fmt.Println(arr)      // [0 0 0 0 0]
	arr = append(arr, 1, 2, 3, 4, 5)
	fmt.Printf("%v  len = %d, cap = %d, %T\n", arr, len(arr), cap(arr),arr)
	Array()
	fmt.Println(topicAddEle(100))
	fmt.Println(ArrSum(topicAddEle(100)))

}

func ArrSum(nums []int) int {
	res := 0
	for _, v := range nums {
		res += v
	}
	return res
}

func topicAddEle(n int) []int {
	arr := make([]int, n)
	ele := 1
	for i := 0; i < n; i++ {
		arr[i] = ele
		ele += 1
	}
	return arr
}

func Array() {
	nums := []int{1, 2, 3, 4, 5, 6}
	nums = append(nums, 7, 8, 9)
	fmt.Println(nums) // [12.9 2 3 4 5 6 7 8 9]

	fmt.Println("nums 的长度: ", len(nums)) // 9
	fmt.Println("nums 的容量: ", cap(nums)) // 12
}
